<?php
namespace Home\Controller;


class NoticeController extends HomeController {
    
    //公告遍历
    public function index(){
       $data = M('Notice')->select();
       $this->assign('title','公告管理');
       $this->assign('list',$data);
       $this->display('Notice/index');
    }

    //公告详情页面
    public function ncontent(){
        $id = I('get.id');
        $list = M('Notice')->where(array('id'=>array('eq',$id)))->select();
        //dump($list);exit;
        $this->assign('ncontent',$ncontent['ncontent']);
        $this->assign('list',$list);
        $this->display();
    }

    //增加
    public function add()
    {
        $this->assign('title','添加公告');
        $this->display('Notice/add');
    }

    public function insert(){
        if (empty($_POST)) {
            $this->error('请填写数据!', U('Notice/add'));
            exit;
        }

        if(!D('Notice')->create()){
                $this->error(D('Notice')->getError());
                exit;
            }
 
        //添加操作,返回自增ID
        if (D('Notice')->add() > 0) {
            $this->success('添加成功',U('index'));
        }else{
            $this->error('添加失败');
        }
    }


    //删除
    public function del()
    {
        
        //接收参数
        $id = I('get.id/d');
        // echo $id;exit;
        if (M('Notice')->delete($id) > 0) {
            $this->success('恭喜您,删除成功!', U('index'));
        } else {
            $this->error('删除失败!');
        }
    }


    //编辑页
    public function edit()
    {
        //接收参数
        $id = I('get.id/d');
        //查找数据
        $data = D('Notice')->find($id);

        $this->assign('title','公告编辑');
        $this->assign('data',$data);
        $this->display('Notice/edit');
    }

    //执行修改
    public function save()
    {
        if (empty($_POST)) {
            $this->redirect('请填写数据!', U('Notice/index'));
            exit;
        }


        
        //生成数据对象
        D('Notice')->create();

        if (D('Notice')->save() > 0) {
            $this->success('修改成功',U('index'));
        } else {
            $this->error('修改失败');
        }
    }

}